Boolean debug = false; // true = debug mode, where text will be printed to the Processing log/console

import processing.serial.*;
Serial AMRport;         // The serial port

// Setup a couple fonts
PFont bigFont;
PFont smallFont;
PFont tinyFont;

// Setup misc variables
String tempString = "";
String saveString = "";

int      stringHistory      = 30;
String[] lastTenStrings     = new String[stringHistory];
String[] lastTenTimes       = new String[stringHistory];
color[]  lastTenColors      = new color[stringHistory];
float[]  spectrogram        = new float[60];
float[]  noiseFloor         = new float[60];
int[]    serialsSeen        = new int[1];
int[]    serialsSeenCounter = new int[1];

boolean  updatedNoiseFloor    = false;

int[]    frequencyList      = {9100,9102,9104,9106,9108,9110,9112,9114,9116,9118,9120,9122,9124,9126,9128,9130,9132,9134,9136,9138,9140,9142,9144,9146,9148,9150,9152,9154,9156,9158,9160,9162,9164,9166,9168,9170,9172,9174,9176,9178,9180,9182,9184,9186,9188,9190,9192,9194,9196,9198,9200,9202,9204,9206,9208,9210,9212,9214,9216,9218};

int      hiliteFreq         = 1000; // Bar column to hilite, 1000 is beyond the range and will therefore hilite none

void setup() {
  size(960, 600); // iPhone Retina web app screen size (landscape), (vertical is 640, 920)
  frameRate(10);  // Set a relatively slow frameRate since it doesn't need to be fast
  
  // Try to auto select the correct port based on its path
  String[] ports = Serial.list();
  if (debug) { println(ports); } // Print a list of available serial ports
  for (int i = 0; i < ports.length; i++) {
    if (match(ports[i], "/dev/tty.usbmodem") != null) {
      AMRport = new Serial(this, Serial.list()[i], 9600);
      break;
    }
  }
  // Don't generate a serialEvent() unless you get a newline character:
  AMRport.bufferUntil('\n');
  
  // Create our fonts
  bigFont   = createFont("SansSerif", 20, true);
  smallFont = createFont("SansSerif", 12, true);
  tinyFont  = createFont("SansSerif", 10, true);
  
  // Initialize our arrays
  for (int i = 0; i < lastTenStrings.length; i++) {
    lastTenStrings[i] = "";
    lastTenTimes[i]   = "";
    lastTenColors[i]  = color(0);
  }
  
  // Turn on smoothing so things look a bit nicer
  smooth();
  
  delay(500);
  serialSetup(); // Setup the AMR to give us the data we want
}

void draw() {
  int timer = millis(); // Create a timer to see how long it takes us to run through our draw() loop
  background(255); // Clear our screen and make it white
  
  // Draw our incoming data
  textFont(smallFont);
  textAlign(LEFT);
  fill(0);
  text("Incoming:", 10, 18);
  for (int i = 0; i < stringHistory; i++) {
    fill(lastTenColors[i]);
    text(lastTenTimes[i],   20, (i * 14) + 36);
    text(lastTenStrings[i], 80, (i * 14) + 36);
  }
  fill(0);
  text("Serials Seen:", 490, 18);
  for (int i = 0; i < serialsSeen.length; i++) {
    text(serialsSeen[i] + "\t" + serialsSeenCounter[i] + " times", 500, (i * 14) + 36);
  }
  
  if (second() % 10 == 0) {
    if (updatedNoiseFloor) {
      
    }
    else {
      getNoiseFloorData();
      updatedNoiseFloor = true;
    }
  }
  else {
    updatedNoiseFloor = false;
  }
  drawGraph(spectrogram, 25, 345, width-50, 100, hiliteFreq, "Received Signals");
  drawGraph(noiseFloor, 25, 480, width-50, 100, hiliteFreq, "Noise Floor");
  
  textAlign(LEFT);
  text("FPS: " + nf(frameRate, 0, 1), 10, height - 2);
  
  // Then draw the resulting time in millis() to show how long it took to go through our draw() loop
  textFont(smallFont);
  fill(128);
  textAlign(RIGHT);
  text((millis() - timer) + " millis draw time", width - 2, height - 2);
}


